package cn.bjxy.shishaolong.ra_room.controller;


import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.ra_room.entity.Raroom;
import cn.bjxy.shishaolong.ra_room.entity.RaroomQueryEntity;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import cn.bjxy.shishaolong.system.user.entity.SysUserInfo;
import cn.bjxy.shishaolong.system.user.service.ISysUserInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 老师答疑室 前端控制器
 * </p>
 *
 * @author 时少龙
 * @since 2019-04-13
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/web/raroom")
public class RaroomController extends RaroomBaseController {


    @Autowired
    private ISysUserInfoService infoService;

    /**
     * TODO 方法加权限验证
     * 获取开通答疑室所需的所有的数据
     *
     * @return
     */
    @RequestMapping("/getAllData")
    @Permission
    public ResultMsg getAllData(HttpServletRequest request) {
        try {
            Integer currentUserId = getCurrentUserId(request);
            SysUserInfo userInfo = infoService.getById(currentUserId);

            // Todo 检查是否已经开启过了


            RaroomQueryEntity allData = raroomService.getAllData();
            allData.setTeacherName(userInfo.getName());
            return ResultMsg.success(allData);
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 条件查询
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryByCondition")
    @Permission
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            Raroom raroom = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                raroom = param.getObject(SysConst.QUERY_CONDITION_KEY, Raroom.class);
            } else {
                raroom = new Raroom(); // 不是管理员  不用查找出已删除的
            }
            raroom.setIsAdmin(SysConst.RA_ROOM_IS_NOT_ADMIN);

            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Raroom> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Raroom> list = raroomService.queryByCondition(page, raroom);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Raroom> list = raroomService.queryByCondition(raroom);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * TODO 方法加权限验证
     * 开通答疑室
     *
     * @return
     */
    @RequestMapping("/open")
    @Permission
    public ResultMsg open(HttpServletRequest request, @RequestBody Raroom raroom) {
        try {
            if (raroom == null) {
                retParamFailure();
            }
            Integer currentUserId = getCurrentUserId(request);
            SysUserInfo userInfo = infoService.getById(currentUserId);

            // Todo 检查是否已经开启过了
            raroom.setTeacherId(currentUserId); // 填写老师的id
            raroomService.open(raroom);
            return retSuccess();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.OPERATOR_FAILURE, StatusCode.OPERATOR_FAILURE_MSG);
        }
    }

    /**
     * TODO 方法加权限验证
     * 关闭答疑室
     *
     * @return
     */
    @RequestMapping("/closeRoom")
    @Permission
    public ResultMsg closeRoom(HttpServletRequest request) {
        try {
            Integer currentUserId = getCurrentUserId(request);
            raroomService.changeRoomStatus(currentUserId,SysConst.RA_ROOM_STATUS_CLOSE);
            return retSuccess();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.OPERATOR_FAILURE, StatusCode.OPERATOR_FAILURE_MSG);
        }
    }

    /**
     * TODO 方法加权限验证
     * 重新开通答疑室
     *
     * @return
     */
    @RequestMapping("/restartRoom")
    @Permission
    public ResultMsg restartRoom(HttpServletRequest request) {
        try {
            Integer currentUserId = getCurrentUserId(request);
            raroomService.changeRoomStatus(currentUserId,SysConst.RA_ROOM_STATUS_OK);
            return retSuccess();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.OPERATOR_FAILURE, StatusCode.OPERATOR_FAILURE_MSG);
        }
    }

    /**
     * 查询个人的答疑室列表
     *
     * @return
     */
    @RequestMapping("/queryMyRaRoomList")
    @Permission
    public ResultMsg queryMyRaRoomList(HttpServletRequest request) {
        try {
            //查询条件
            Integer currentUserId = getCurrentUserId(request);
            Map map = raroomService.queryMyRaRoomList(currentUserId);
            return ResultMsg.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 查询答疑室的所属者
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryRoomOwn")
    @Permission
    public ResultMsg queryRoomOwn(HttpServletRequest request, @RequestBody JSONObject param) {
        try {
            // 获取roomId
            if(!param.containsKey(ROOM_ID) || param.getInteger(ROOM_ID) == null){
                retParamFailure();
            }
            Integer roomId = param.getInteger(ROOM_ID);
            Integer currentUserId = getCurrentUserId(request);
            Map map = raroomService.queryRoomOwn(currentUserId,roomId);
            return ResultMsg.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

}
